package org.jeecg.modules.demo.jxc.baseData.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.jxc.baseData.entity.JxcSupplier;
import org.jeecg.modules.demo.jxc.baseData.service.IJxcSupplierService;
import org.jeecg.modules.jxc.baseData.entity.JxcGoodsBo;
import org.jeecg.modules.jxc.baseData.vo.VO;
import org.jeecg.modules.jxc.homepage.service.IJxcGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: jxc_supplier
 * @Author: jeecg-boot
 * @Date:   2021-12-20
 * @Version: V1.0
 */
@Api(tags="jxc_supplier")
@RestController
@RequestMapping("/jxc.baseData/jxcSupplier")
@Slf4j
public class JxcSupplierController extends JeecgController<JxcSupplier, IJxcSupplierService> {
	@Autowired
	private IJxcSupplierService jxcSupplierService;

	 @Autowired
	 private IJxcGoodsService jxcGoodsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param jxcSupplier
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "jxc_supplier-分页列表查询")
	@ApiOperation(value="jxc_supplier-分页列表查询", notes="jxc_supplier-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(JxcSupplier jxcSupplier,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<JxcSupplier> queryWrapper = QueryGenerator.initQueryWrapper(jxcSupplier, req.getParameterMap());
		Page<JxcSupplier> page = new Page<JxcSupplier>(pageNo, pageSize);
		IPage<JxcSupplier> pageList = jxcSupplierService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param jxcSupplier
	 * @return
	 */
	@AutoLog(value = "jxc_supplier-添加")
	@ApiOperation(value="jxc_supplier-添加", notes="jxc_supplier-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody JxcSupplier jxcSupplier) {
		jxcSupplierService.save(jxcSupplier);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param jxcSupplier
	 * @return
	 */
	@AutoLog(value = "jxc_supplier-编辑")
	@ApiOperation(value="jxc_supplier-编辑", notes="jxc_supplier-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody JxcSupplier jxcSupplier) {
		jxcSupplierService.updateById(jxcSupplier);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "jxc_supplier-通过id删除")
	@ApiOperation(value="jxc_supplier-通过id删除", notes="jxc_supplier-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		jxcSupplierService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "jxc_supplier-批量删除")
	@ApiOperation(value="jxc_supplier-批量删除", notes="jxc_supplier-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.jxcSupplierService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "jxc_supplier-通过id查询")
	@ApiOperation(value="jxc_supplier-通过id查询", notes="jxc_supplier-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		JxcSupplier jxcSupplier = jxcSupplierService.getById(id);
		if(jxcSupplier==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(jxcSupplier);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param jxcSupplier
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JxcSupplier jxcSupplier) {
        return super.exportXls(request, jxcSupplier, JxcSupplier.class, "jxc_supplier");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JxcSupplier.class);
    }

	/**
	* @description: 关键字查询
	* @param kewWord
	* @param pageNo
	* @param pageSize
	* @return org.jeecg.common.api.vo.Result<?>
	* @exception
	*/
    @GetMapping("/selectByKey")
	public Result<?> selectByKeyWord(@RequestParam String kewWord,
									 @RequestParam Integer pageNo,
									 @RequestParam Integer pageSize){
		QueryWrapper<JxcSupplier> queryWrapper = new QueryWrapper<>();
		Page<JxcSupplier> page = new Page<JxcSupplier>(pageNo, pageSize);
		IPage<JxcSupplier> pageList = jxcSupplierService.page(page, queryWrapper.like("supplier_name",kewWord));
		return Result.ok(pageList);
	}

	/**
	* @description: 查询有库存商品
	* @param key
	* @param pageNo
	* @param pageSize
	* @return org.jeecg.common.api.vo.Result<?>
	* @exception
	*/
	 @AutoLog(value = "期初库存分页列表查询")
	 @ApiOperation(value="期初库存分页列表查询", notes="期初库存分页列表查询")
	 @GetMapping(value = "/openingInventory")
	 public Result<?> queryPageList(@RequestParam(value = "key",required = false)String key,
	 								@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize
									) {
		 VO<JxcGoodsBo> vo = jxcGoodsService.queryGoodsList(key,pageNo,pageSize);

		 return Result.ok(vo);
	 }

	 /**
	  * @description: 查询无库存商品
	  * @param key
	  * @param pageNo
	  * @param pageSize
	  * @return org.jeecg.common.api.vo.Result<?>
	  * @exception
	  */
	 @AutoLog(value = "期初库存分页列表查询")
	 @ApiOperation(value="期初库存分页列表查询", notes="期初库存分页列表查询")
	 @GetMapping(value = "/nullOpeningInventory")
	 public Result<?> queryNullPageList(@RequestParam(value = "key",required = false)String key,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize
	 ) {
		VO<JxcGoodsBo> vo = jxcGoodsService.queryNullGoodsList(key,pageNo,pageSize);
	 	return Result.ok(vo);
	 }

	 /**
	 * @description: 删除商品库存
	 * @param id
	 * @return org.jeecg.common.api.vo.Result<?>
	 * @exception
	 */
	 @AutoLog(value = "删除库存")
	 @ApiOperation(value="删除库存", notes="删除库存")
	 @DeleteMapping(value = "/deleteGoods")
	 public Result<?> queryNullPageList(@RequestParam(value = "id")Integer id) {
		 jxcGoodsService.changeGoodsNumToNull(id);
		 return Result.ok();
	 }
}
